package com.ruoyi.basice_info.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.basice_info.domain.TsExamSyllabusVo;
import com.ruoyi.basice_info.mapper.TsExamSyllabusVoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basice_info.mapper.TbTeachingResearchSectionMapper;
import com.ruoyi.basice_info.domain.TbTeachingResearchSection;
import com.ruoyi.basice_info.service.ITbTeachingResearchSectionService;

/**
 * 教研组设置Service业务层处理
 *
 * @author hongyi
 * @date 2023-12-08
 */
@Service
public class TbTeachingResearchSectionServiceImpl implements ITbTeachingResearchSectionService
{
    @Autowired
    private TbTeachingResearchSectionMapper tbTeachingResearchSectionMapper;

    @Autowired
    private TsExamSyllabusVoMapper tsExamSyllabusVoMapper;

    /**
     * 查询教研组设置
     *
     * @param teachingResearchSectionId 教研组设置主键
     * @return 教研组设置
     */
    @Override
    public TbTeachingResearchSection selectTbTeachingResearchSectionByTeachingResearchSectionId(Long teachingResearchSectionId)
    {
        return tbTeachingResearchSectionMapper.selectTbTeachingResearchSectionByTeachingResearchSectionId(teachingResearchSectionId);
    }

    @Override
    public TbTeachingResearchSection getTbTeachingResearchSectionByTeachingResearchSectionCode(String code) {
        return tbTeachingResearchSectionMapper.getTbTeachingResearchSectionByTeachingResearchSectionCode(code);
    }

    @Override
    public TbTeachingResearchSection getTbTeachingResearchSectionByTeachingResearchSectionName(String name) {
        return tbTeachingResearchSectionMapper.getTbTeachingResearchSectionByTeachingResearchSectionName(name);
    }

    /**
     * 查询教研组设置列表
     *
     * @param tbTeachingResearchSection 教研组设置
     * @return 教研组设置
     */
    @Override
    public List<TbTeachingResearchSection> selectTbTeachingResearchSectionList(TbTeachingResearchSection tbTeachingResearchSection)
    {
        return tbTeachingResearchSectionMapper.selectTbTeachingResearchSectionList(tbTeachingResearchSection);
    }

    /**
     * 新增教研组设置
     *
     * @param tbTeachingResearchSection 教研组设置
     * @return 结果
     */
    @Override
    public int insertTbTeachingResearchSection(TbTeachingResearchSection tbTeachingResearchSection)
    {
        return tbTeachingResearchSectionMapper.insertTbTeachingResearchSection(tbTeachingResearchSection);
    }

    /**
     * 修改教研组设置
     *
     * @param tbTeachingResearchSection 教研组设置
     * @return 结果
     */
    @Override
    public int updateTbTeachingResearchSection(TbTeachingResearchSection tbTeachingResearchSection)
    {
        return tbTeachingResearchSectionMapper.updateTbTeachingResearchSection(tbTeachingResearchSection);
    }

    /**
     * 批量删除教研组设置
     *
     * @param teachingResearchSectionIds 需要删除的教研组设置主键
     * @return 结果
     */
    @Override
    public int deleteTbTeachingResearchSectionByTeachingResearchSectionIds(Long[] teachingResearchSectionIds)
    {

         int num = -1;
         int nums = 0;
        List<Long> longList = Arrays.asList(teachingResearchSectionIds);
        // 进行循环遍历
        for(Long item: longList) {
            TsExamSyllabusVo syllabusVo = new TsExamSyllabusVo();
            syllabusVo.setTeachingResearchSectionId(item);
            // 去子表中查询
            List<TsExamSyllabusVo> list = tsExamSyllabusVoMapper.selectTsExamSyllabusList(syllabusVo);
            if(list.size()<=0) {
                num = tbTeachingResearchSectionMapper.deleteTbTeachingResearchSectionByTeachingResearchSectionId(item);
            }else  {
                nums ++;
            }
        }
        if (longList.size() == 1) {
            if (nums > 0) {
                throw new IllegalArgumentException("该数据在考试大纲设置中使用，不能删除");
            } else {
                if(nums > 0) {
                    throw new IllegalArgumentException(nums + " 条数据删除失败，数据在考试大纲设置中使用，不能删除");
                }
            }
        }

//        return tbTeachingResearchSectionMapper.deleteTbTeachingResearchSectionByTeachingResearchSectionIds(teachingResearchSectionIds);
     return num;
    }

    /**
     * 删除教研组设置信息
     *
     * @param teachingResearchSectionId 教研组设置主键
     * @return 结果
     */
    @Override
    public int deleteTbTeachingResearchSectionByTeachingResearchSectionId(Long teachingResearchSectionId)
    {
        return tbTeachingResearchSectionMapper.deleteTbTeachingResearchSectionByTeachingResearchSectionId(teachingResearchSectionId);
    }
}
